<?php
namespace app\admin\controller;

use app\admin\controller\AdminBase;

use app\model\PageConfig as C;
use think\facade\Db;

class Config extends AdminBase
{

    private $ppage_type = 'page_type/d';
    private $pimg = 'img/s';    
    private $pitem_id = 'item_id/d';
    private $ptitle = 'title/s';

    public function index(string $do = '')
    {
        if ($do == 'json') {
            $where = [];
            $kw  = $this->request->param('kw');
            if ($kw) $where[] = ['title','like','%'.$kw.'%'];
            $list = (new C())->listQuery($where);
            $list = $list->toArray();
            foreach ($list['data'] as  &$v) {
                $v['page_type_text'] = $v['page_type'] == 1 ? '活动详情' : ($v['page_type'] == 2 ? '展览' : '作品');
            }
            return $this->returnMsg($list,code: 0);
        }
        $this->assign(['limit'=>10]); 
        return $this->fetch();
    }

    public function bedit(string $do = '')
    {
        $d = $this->only($do ? ['@token'=>'','id/d/ID参数错误','av','af'] : ['@token'=>'','id/d/ID参数错误', $this->ptitle,$this->pimg,$this->ppage_type,$this->pitem_id]);
        $id = $d['id'];
        $Myobj = C::get("id = $id");
        if(!$Myobj) return $this->returnMsg("数据不存在");
        if ($do == 'up') {
            $value = $d['av'];
            $field = $d['af'];
            if(!in_array($field,['title','img','page_type','item_id'])) return $this->returnMsg("参数错误");
            if($Myobj->save([$field=>$value])){
                return $this->returnMsg("设置成功", code: 1);
            }
        }else{
            if($Myobj->save($d)){
                return $this->returnMsg("编辑成功", code: 1);
            }
        }
    }

    public function badd()
    {
        $d = $this->only(['@token'=>'id/d/ID参数错误',$this->ptitle,$this->ppage_type,$this->pitem_id,$this->pimg]);
        if((new C())->save($d)){
            return $this->returnMsg("添加成功", code: 1);
        }else{
            return $this->returnMsg("添加失败");
        }
    }

        //bdel 删除
        public function bdel()
        {
            $id = $this->only(['@token'=>'','id'])['id'];
            $id = is_array($id) ? implode(',',$id) : $id;
            if(!$id) return $this->returnMsg('参数错误');
            $ids = explode(',', $id);
            if((new C())->where('id','in',$id)->delete()){
                return $this->returnMsg("删除成功", code: 1);
            }else{
            return $this->returnMsg("删除失败");
        }
    }
    
}